<?php
 if(!defined('IN_CAI')) {
	exit('Access Denied');
}
/**
 * 公告管理
 *
 */
$ac = $_REQUEST['ac'];
$url = '/admin/index.php?do=news';
if (empty($ac)) {
	$ac = 'list';
}
if ($ac == 'list') {
	$page = intval($_GET['page']) >0 ? intval($_GET['page']) : 1;
	$limit = 20;
	$offset = ($page - 1) *$limit;
	$count = getcount('news',array());
	$query = $db->query("select * from ".tname('news')." order by id desc limit $offset,$limit");
	$list = $db->fetch_all($query);
	$pagestr = multi($count,$limit,$page,$url);
	include atemplate('do_news_list');
} elseif ($ac == 'add' || $ac == 'edit') {

	if (empty($_POST['step'])) {
		$id = intval($_GET['id']);
		if ($ac == 'edit' && $id > 0) {
			$query = $db->query("select * from ".tname('news')." where id = $id");
			$info = $db->fetch_array ($query);
			if (empty($info)) {
				adminmessage('对不起，找不到改信息');
			}
		}
		include atemplate('do_news_add');
	}else {
		$id = intval($_POST['id']);
		$content = trim($_POST['content']);
		$title = trim($_POST['title']);
		$data['title'] = $title;
		$data['content'] = $content;
		if (empty($data['content'])) {
			adminmessage('请填写公告内容');
		}
		if (empty($data['title'])) {
			adminmessage('请填写公告标题');
		}
		$data['posttime'] = time();
		if (!$id) {
			$data['createtime'] = time();
			inserttable('news',$data);
			adminmessage('添加公告成功',$url);
		}else{
			updatetable('news',$data,array('id'=>$id));
			adminmessage('修改公告成功',$url);
		}
	}
} elseif ($ac == 'del') {
	$id = intval($_GET['id']);
	if (!id) {
		adminmessage('非法ID');
	} else {
		$db->query('delete from '.tname('news').' where id='.$id);
		if ($db->affected_rows()) {
			adminmessage('删除公告成功',$url);
		}else {
			adminmessage('删除公告失败',$url);
		}
	}
}
?>